package com.smileus.common;

import java.io.Serializable;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 描述一台服务器的网络异常尝试次数
 * @author heng.dong
 *
 */
public class TryTimes implements Serializable {
	private static final long serialVersionUID = 1L;
	private AtomicInteger curtTimes;
	private int maxTimes;

	public TryTimes(int maxTimes) {
		super();
		this.curtTimes = new AtomicInteger(0);// 初始值是0
		this.setMaxTimes(maxTimes);
	}

	public AtomicInteger getCurtTimes() {
		return curtTimes;
	}

	public void setCurtTimes(AtomicInteger curtTimes) {
		this.curtTimes = curtTimes;
	}

	public void setMaxTimes(int maxTimes) {
		this.maxTimes = maxTimes;
	}

	public int getMaxTimes() {
		return maxTimes;
	}

	public int compareToMax() {
		return curtTimes.get() - maxTimes;
	}

	/**
	 * 增加尝试次数值，同时检查与最大尝试值的比较，如果超过了，返回true
	 * 此时启动备用机器
	 * @return
	 */
	public boolean incrementAndCheck() {
		return (curtTimes.incrementAndGet() >= maxTimes ? true : false);
	}
}
